package de.zainodis.kitchengnome.domainmodel.persistence;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.DatabaseTableConfig;
import com.j256.ormlite.table.TableUtils;

import de.zainodis.commons.LogCat;
import de.zainodis.kitchengnome.domainmodel.Suggestion;

/**
 * Handles persistence of word suggestions.
 * 
 * @author fzarrai
 * 
 */
public interface ISuggestionDao extends Dao<Suggestion, String> {

	/**
	 * Creates a suggestion with the given name, if none exists yet.
	 * 
	 * @param suggestions
	 *           the suggestion to save.
	 * @return true if saving the given suggestion was successful; false
	 *         otherwise.
	 */
	boolean save(Suggestion suggestions);

	/**
	 * Clears all suggestions.
	 */
	boolean clear();

	/**
	 * Deletes the given suggestion.
	 */
	int remove(Suggestion suggestion);

	/**
	 * @return all suggestions.
	 */
	List<Suggestion> getSuggestions();

	/**
	 * @return all suggestions.
	 */
	List<String> getStringSuggestions();

	public class SuggestionDao extends BaseDaoImpl<Suggestion, String> implements ISuggestionDao {

		private static final String TAG = "SuggestionDao";

		public SuggestionDao(ConnectionSource connectionSource) throws SQLException {
			super(connectionSource, Suggestion.class);
		}

		public SuggestionDao(ConnectionSource connectionSource, DatabaseTableConfig<Suggestion> tableConfig)
				throws SQLException {
			super(connectionSource, tableConfig);
		}

		@Override
		public boolean save(Suggestion suggestions) {
			if (suggestions != null) {
				try {
					createIfNotExists(suggestions);
				} catch (SQLException e) {
					LogCat.e(TAG, "Failed to save " + suggestions.getName(), e);
					return false;
				}
			}
			// If the suggestion is null its not considered a failure
			return true;
		}

		@Override
		public boolean clear() {
			try {
				TableUtils.clearTable(connectionSource, Suggestion.class);
			} catch (SQLException e) {
				LogCat.e(TAG, "Failed to clear table.", e);
				return false;
			}
			return true;
		}

		@Override
		public int remove(Suggestion suggestion) {
			if (suggestion != null) {
				try {
					return delete(suggestion);
				} catch (SQLException e) {
					LogCat.e(TAG, "Failed to delete " + suggestion.getName(), e);
				}
			}
			return 0;
		}

		@Override
		public List<Suggestion> getSuggestions() {
			try {
				return super.queryForAll();

			} catch (SQLException e) {
				LogCat.e(TAG, "Failed load suggestions.", e);
			}
			return new ArrayList<Suggestion>();
		}

		@Override
		public List<String> getStringSuggestions() {
			List<Suggestion> suggestions = getSuggestions();
			List<String> result = new ArrayList<String>();

			for (Suggestion suggestion : suggestions) {
				result.add(suggestion.getName());
			}
			return result;
		}
	}
}
